//
// Created by LVZHE on 2023/3/7.
//

#include <snake.h>
Snake snake;
Food food;
char now_Dir=RIGHT;
char direction=RIGHT;

int Menu(){
	GotoXY(40,12);
	printf("欢迎来到贪吃蛇小游戏！");
	GotoXY(43,14);
	printf("1.开始游戏");
	GotoXY(43,16);
	printf("2.帮助");
	GotoXY(43,18);
	printf("3.关于");
	GotoXY(43,20);
	printf("其它任意键推出游戏");
	Hide();
	char ch;
	int result=0;
	ch=_getch();
	switch (ch) {
		case '1': result=1;break;
		case '2': result=2;break;
		case '3': result=3;break;
	}
	system("cls");
	return result;
}

void GotoXY(int x,int y){
	HANDLE hout;
	COORD cor;
	hout= GetStdHandle(STD_OUTPUT_HANDLE);
	cor.X=x;
	cor.Y=y;
	SetConsoleCursorPosition(hout,cor);
}

void Hide(){
	HANDLE hout= GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cor_info={1,0};
	SetConsoleCursorInfo(hout,&cor_info);
}

void About(){
	GotoXY(30,12);
	printf("杭州电子科技大学——程序综合设计实践案例");
	GotoXY(43,14);
	printf("贪吃蛇游戏");
	GotoXY(43,16);
	printf("按任意键返回上级菜单");
	Hide();
	char ch=_getch();
	system("cls");
}

void Help(){
	GotoXY(40,12);
	printf("w 上");
	GotoXY(40,14);
	printf("s 下");
	GotoXY(40,16);
	printf("a 左");
	GotoXY(40,18);
	printf("d 右");
	GotoXY(40,20);
	printf("撞墙结束");
	GotoXY(45,22);
	printf("按任意键返回上级菜单");
	Hide();
	char ch=_getch();
	system("cls");
}

void InitMap(){
	Hide();
	snake.snakenode[0].x=MAP_WIDTH / 2 - 1;
	snake.snakenode[0].y=MAP_WIDTH / 2 - 1;
	GotoXY(snake.snakenode[0].x,snake.snakenode[0].y);
	printf("@");
	snake.length=3;
	snake.speed=250;
	now_Dir=RIGHT;
	for(int i=1; i<snake.length; i++){
		snake.snakenode[i].y=snake.snakenode[i-1].x;
		snake.snakenode[i].x=snake.snakenode[i-1].x-1;
		GotoXY(snake.snakenode[i].x,snake.snakenode[i].y);
		printf("o");
	}
	for (int i = 0; i < MAP_WIDTH; ++i) {
		GotoXY(0,i);
		printf("-");
		GotoXY(MAP_HEIGHT-1,i);
		printf("-");
	}
	for (int i = 0; i < MAP_HEIGHT; ++i) {
		GotoXY(i,0);
		printf("|");
		GotoXY(i,MAP_WIDTH-1);
		printf("|");
	}
	PrintFood();
	GotoXY(5,50);
	printf("当前得分：0");
}

void PrintFood(){
	int flag=1;
	while (flag){
		flag=0;
		food.x=rand() % (MAP_WIDTH-2)+1;
		food.y=rand() % (MAP_HEIGHT-2)+1;
		for (int k = 0; k <= snake.length-1 ; ++k) {
			if (snake.snakenode[k].x == food.x && snake.snakenode[k].y == food.y){
				flag = 1;
				break;
			}
		}
	}
	GotoXY(food.x,food.y);
	printf("$");
}

int MoveSnake(){
	Snakenode  temp;
	int flag = 0;
	temp = snake.snakenode[snake.length-1];
	for (int i = snake.length-1; i >= 1 ; --i) {
		snake.snakenode[i] = snake.snakenode[i-1];
	}
	GotoXY(snake.snakenode[1].x,snake.snakenode[1].y);
	printf("o");
	if (_kbhit()){
		direction = _getch();
		switch (direction) {
		case UP:
			if (now_Dir != DOWN){
				now_Dir = direction;
			}
			break;
		case DOWN:
			if (now_Dir != UP){
				now_Dir = direction;
			}
			break;
		case LEFT:
			if (now_Dir != RIGHT){
				now_Dir = direction;
			}
			break;
		case RIGHT:
			if (now_Dir != LEFT){
				now_Dir = direction;
			}
			break;
		}
	}
	switch (now_Dir) {
		case UP: snake.snakenode[0].y--;break;
		case DOWN: snake.snakenode[0].y++;break;
		case LEFT: snake.snakenode[0].x--;break;
		case RIGHT: snake.snakenode[0].x++;break;
	}
	GotoXY(snake.snakenode[0].x,snake.snakenode[0].y);
	printf("@");
	if (snake.snakenode[0].x == food.x && snake.snakenode[0].y == food.y){
		snake.length++;
		flag = 1;
		snake.snakenode[snake.length-1] = temp;
	}
	if (!flag){
		GotoXY(temp.x,temp.y);
		printf(" ");
	} else{
		PrintFood();
		GotoXY(50,5);
		printf("当前得分：%d",snake.length-3);
	}
	if (!IsCorrect()){
		system("cls");
		GotoXY(45,14);
		printf("最终得分：%d",snake.length-3);
		GotoXY(45,16);
		printf("你输了");
		GotoXY(45,18);
		printf("按任意键返回主菜单");
		char c = _getch();
		system("cls");
		return 0;
	}
	SpeedControl();
	Sleep(snake.speed);
	return 1;
}
int IsCorrect(){
	if(snake.snakenode[0].x == 0 || snake.snakenode[0].y == 0 || snake.snakenode[0].x == MAP_WIDTH-1 || snake.snakenode[0].y == MAP_HEIGHT-1) {
		return 0;
	}
	for (int i = 1; i < snake.length ; ++i) {
		if (snake.snakenode[0].x == snake.snakenode[i].x && snake.snakenode[0].y == snake.snakenode[i].y){
			return 0;
		}
		return 1;
	}
}
void SpeedControl(){
	switch (snake.length) {
		case 6: snake.speed = 200;break;
		case 9: snake.speed = 180;break;
		case 12: snake.speed = 160;break;
		case 15: snake.speed = 140;break;
		case 18: snake.speed = 120;break;
		case 21: snake.speed = 100;break;
		case 24: snake.speed = 80;break;
		case 27: snake.speed = 60;break;
		case 30: snake.speed = 40;break;
	default:
		break;
	}
}
